package com.cy;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cy.server.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 服务消费方对象的启动类
 * 业务描述：
 *     当客户端（浏览器，手机app）向服务消费方发起请求时，
 *     服务消费方调用服务提供方的api，进而获取服务提供方的数据
 *  例如：
 *     我们访问一个订单模块数据（例如我的订单）,订单模块中还要呈现商品信息
 */
@EnableFeignClients
@SpringBootApplication
public class ConsumerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConsumerApplication.class, args);

    }

    @Bean
    //@Bean("rt")
    public RestTemplate restTemplate(){
        System.out.println("===restTemplate()===");
        return new RestTemplate();
    }

    @Bean
    @LoadBalanced
    public RestTemplate loadBalancerTemplate(){
        return new RestTemplate();
    }

    //负载均衡客户端对象
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RestController
    public class ConsumerController{

        @Autowired
        private RestTemplate restTemplate;

        @Autowired
        private RestTemplate loadBalancerTemplate;

        @Value("${spring.application.name}")
        private String appName;

        @Autowired
        private ConsumerService consumerService;

        //http://ip:port/consumer/findById?id=10
        @GetMapping("/consumer/findById")
        @SentinelResource("res")
        public String doFindById(Integer id){
            return "resource id is "+id;
        }

        //构建一个线程安全并且可实现自增自减操作的整数对象
        private AtomicLong aLong = new AtomicLong(1);

        //http://ip:port/consumer/doRestEcho1
        @GetMapping("/consumer/doRestEcho1")
        public String doRestEcho1() throws InterruptedException {

            long num = aLong.getAndIncrement();
            if(num%2 == 0){
                //系统设计时会认为响应设计超过200毫秒为慢调用
                Thread.sleep(200);//模拟耗时操作
            }

            //直接通过业务方法访问相关资源（现在不需要关心什么返回值）
//            consumerService.deGetResource();
//            System.out.println("====doRestEcho1=====");

//            System.out.println("=====doRestEcho1()====");

            //调用服务提供方API（http://ip:port/path）
            //1.定义要调用的API
            String url = "http://localhost:8081/provider/echo/"+appName;

            //2.谁去访问这个API
            //RestTemplate

            return restTemplate.getForObject(url, String.class);

        }


        /**
         * 负载均衡调用
         * @return
         */
        @GetMapping("/consumer/doRestEcho2")
        public String doRestEcho2(){

            //1.从注册中心获取服务实力
            ServiceInstance instance = loadBalancerClient.choose("sca-provider");

            //2.基于RestTemplate进行服务实例调用
            String ip = instance.getHost();//ip
            int port = instance.getPort();
            //String url = "http://"+ip+":"+port+"/provider/echo/"+appName;
            String url = String.format("http://%s:%s/provider/echo/%s",ip,port,appName);
            return restTemplate().getForObject(url,String.class);

        }

        @GetMapping("/consumer/doRestEcho3")
        public String doRestEcho3(){

            //定义url
            String url = String.format("http://sca-provider/provider/echo/%s",appName);

            //服务调用
            return loadBalancerTemplate.getForObject(url, String.class );
        }
    }
}
